import { allAppList } from "@/components/workApp/components/appList";
import { findAllAppNew_axios } from "@/api/wordbench";

const action = {
  getAllApps(store, userId){


    function checkUseful(text, list){
      let row = list.find((item)=>{
        return item.text === text
      })
      return !!row
    }

    findAllAppNew_axios({ userid: userId }).then((res) => {
      let arr = Array.isArray(res) ? res : []
      let fu = arr.find((item)=>{
        return item.NAME === '我的常用';
      })
      let appList = fu ? (fu.CHILD||[]).map((item)=>{
        let ainfo = allAppList.find((appItem)=>{
          return appItem.text === item.NAME
        })
        return {
          sort: item.sortNum,
          id: item.ID,
          pid: item.PID,
          text: item.NAME,
          frequentlyUsed: true,
          img: ainfo ? ainfo.img : '',
        }
      }) : []
      store.commit('setFrequentlyUsedAppList',appList); //常用应用

      let partitionList = arr.filter((item)=>{
        return item.NAME !== '更多' && item.NAME !== '我的常用';
      }).map((item)=>{
        let appItem = allAppList.find((aItem)=>{
          return aItem.type === item.NAME;
        })
        let apps = [];
        (item.CHILD||[]).forEach((ch)=>{
          let childObj = allAppList.find((chi)=>{
            return chi.text === ch.NAME;
          })
          if(childObj){
            apps.push({
              ...childObj,
              sort: ch.sortNum,
              id: ch.ID,
              pid: ch.PID,
              frequentlyUsed: checkUseful(ch.NAME,appList)
            });
          }
        })
        return {
          id: item.ID,
          pid: item.PID,
          active: false,
          text: item.NAME,
          sortNum: item.SORT_NUM,
          img: appItem ? appItem.typeImg : '',
          apps: apps,
        };
      })

      store.commit('setPartitionList',partitionList);  //全部应用
    })
  },
};

export default action;
